package com.jiangnanxiaoguan.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jiangnanxiaoguan.common.BaseContext;
import com.jiangnanxiaoguan.common.Result;
import com.jiangnanxiaoguan.entity.ShoppingCart;
import com.jiangnanxiaoguan.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author 不愿透露姓名的狂热者
 * @since 2022-09-11
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 根据用户id查询购物车信息。
     *
     * @return
     */
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list() {
        // 用户id
        Long currentUserId = BaseContext.getCurrentId();
        // 条件查询
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, currentUserId)
                                      .orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(shoppingCartLambdaQueryWrapper);
        return Result.success(shoppingCartList);
    }

    /**
     * 添加套餐或菜品到购物车。
     *
     * @param shoppingCart
     */
    @PostMapping("/add")
    public Result<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info(shoppingCart.toString());
        // 哪个用户的购物车记录
        Long currentUserId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentUserId);

        // 菜品还是套餐
        Long dishId = shoppingCart.getDishId();
        // 通用条件(当前用户id)
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, currentUserId);
        if (dishId != null) {
            // 菜品
            shoppingCart.setDishId(dishId);
            // 根据菜品id查询
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            // 套餐
            Long setmealId = shoppingCart.getSetmealId();
            shoppingCart.setSetmealId(setmealId);
            // 根据套餐id查询
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId, setmealId);
        }

        ShoppingCart shoppingCartInDB = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
        // 更新还是添加
        if (shoppingCartInDB != null) {
            // 更新
            Integer number = shoppingCartInDB.getNumber();
            shoppingCartInDB.setNumber(number + 1);
            shoppingCartService.updateById(shoppingCartInDB);
        } else {
            // 添加
            shoppingCart.setNumber(1); // 前端没有传递数量
            shoppingCart.setCreateTime(LocalDateTime.now()); // 单独设置加入购物车的时间
            shoppingCartService.save(shoppingCart);
            shoppingCartInDB = shoppingCart; // 返回参数统一
        }

        return Result.success(shoppingCartInDB);
    }

    /**
     * 根据id更新购物车或删除购物车记录。
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public Result<ShoppingCart> subtract(@RequestBody ShoppingCart shoppingCart) {
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        log.info("菜品id：{}", dishId);
        log.info("套餐id：{}", setmealId);

        // 通用条件
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        // 菜品还是套餐
        if (dishId != null) {
            // 菜品(条件)
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            // 套餐(条件)
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId, setmealId);
        }
        // 查询
        ShoppingCart shoppingCartInDB = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
        // 更新还是删除
        if (shoppingCartInDB != null) {
            Integer number = shoppingCartInDB.getNumber();
            if (number > 1) {
                // 更新
                shoppingCartInDB.setNumber(number - 1);
                shoppingCartService.updateById(shoppingCartInDB);
            } else {
                // 删除
                shoppingCartService.removeById(shoppingCartInDB.getId());
                shoppingCartInDB = shoppingCart;
            }
        }
        return Result.success(shoppingCartInDB);
    }

    /**
     * 根据用户id清空购物车。
     * @return
     */
    @DeleteMapping("/clean")
    public Result<String> cleanCart() {
        // 用户id(清空条件)
        Long currentUserId = BaseContext.getCurrentId();
        // 构造清空条件对象
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, currentUserId);
        shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
        return Result.success("清空购物车成功");
    }

}

